/*
@Filename: ex801.c
@Author: Ju Chengdong
@Version: 1.0
@Date: 2021-03-18
@Description: File Character Reading and Writing
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char *argv[]){
	/*锛?锛夊０鏄庡嚱鏁板強鍙橀噺*/
	int writeToFile(char *str, char *fileName, char *mode);
	
	char str[100]; 
	char fileName[] = "ex801.txt";
	
	/*锛?锛夎幏鍙栭敭鐩樿緭鍏ュ瓧涓?/
	fgets(str, 100, stdin);
	//gets(str); //灏嗗洖杞︾湅浣滃瓧涓茶緭鍏ョ粨鏉熸爣蹇楋紝瀛椾覆涓彲浠ユ湁绌烘牸 
	//scanf("%s", str); //灏嗙┖鏍肩湅浣滃瓧涓茶緭鍏ョ粨鏉熸爣蹇楋紝瀛椾覆涓笉鑳芥湁绌烘牸 
	
	/*锛?锛夊皢瀛椾覆鍐欏叆鏂囦欢*/
	int charNum = writeToFile(str, fileName, "w");
	if(charNum < 0){
		//printf("write error");//鐢ㄤ簬璋冭瘯 
		return -1;
	}
	
	return 0;
}

/*
 * 鍑芥暟鍚嶇О锛歸riteToFile
 * 鍑芥暟鍔熻兘锛氬皢瀛椾覆鍐欏叆鏂囦欢 
 * 褰㈠紡鍙傛暟锛歝har *str锛屼竴缁村瓧绗︽暟缁勶紙瀛楃涓诧級棣栧湴鍧€
 * 褰㈠紡鍙傛暟锛歝har *fileName锛屽緟鍐欏叆鐨勬枃浠惰矾寰勫強鍚嶇О
 * 褰㈠紡鍙傛暟锛歝har *mode锛屾枃浠朵娇鐢ㄦ柟寮?
 * 杩?鍥?鍊硷細int鍨嬶紝鑻ユ枃浠舵墦寮€寮傚父锛岃繑鍥?-1锛涘惁鍒欒繑鍥炲啓鍏ュ埌鏂囦欢鐨勫瓧绗︽暟 
 */
int writeToFile(char *str, char *fileName, char *mode){
	// 璇风紪绋嬪疄鐜版湰鍑芥暟
	
	// 璇风紪绋嬪疄鐜版湰鍑芥暟
	FILE *fp;
	fp=fopen(fileName,mode);
	int x='A'-'a';
	for(int i=0;i<strlen(str);i++)
	{
		if(str[i]<='Z'&&str[i]>='A')str[i]-=x;
		else if(str[i]<='z'&&str[i]>='a')str[i]+=x;
	}
	fprintf(fp,"%s",str);
}
